<?php

namespace App\Models\User;

use App\Models\Log\LogPayShowContact;
use App\Models\Log\LogUserBrowse;
use App\Models\Log\LogUserCollect;
use App\Models\Search\SearchResult;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;


use App\Models\Log\LogUserFund;
use App\Models\Log\LogUserOperation;


class Users extends Model
{
    use HasDateTimeFormatter;

//    use SoftDeletes;

    protected $guarded = [];

    protected static function boot()
    {
        parent::boot(); // TODO: Change the autogenerated stub
        static::deleted(function ($model) {
            UserDetail::where('id', $model->id)->delete();
            UserFunds::where('id', $model->id)->delete();
            LogUserFund::where('uid', $model->id)->delete();
            LogUserBrowse::where('uid', $model->id)->delete();
            LogUserBrowse::where('result_id', "i" . $model->id)->delete();
            LogUserCollect::where('uid', $model->id)->delete();
            LogUserCollect::where('result_id', "i" . $model->id)->delete();
            LogUserOperation::where('uid', $model->id)->delete();
            LogPayShowContact::where('uid', $model->id)->delete();
            LogPayShowContact::where('search_id', $model->id)->delete();
            SearchResult::where('search_id', $model->id)->delete();
            SearchResult::where('result_id', $model->id)->where('type', 'i')->delete();
        });
    }

    public function funds()
    {
        return $this->hasOne(UserFunds::class, 'id', 'id');
    }

    public function detail()
    {
        return $this->hasOne(UserDetail::class, 'id', 'id');
    }

    public function parent()
    {
        return $this->hasOne(self::class, 'id', 'parent_id');
    }

    public function log_fund()
    {
        return $this->hasMany(LogUserFund::class, 'id', 'uid');
    }

    public function log_operation()
    {
        return $this->hasMany(LogUserOperation::class, 'id', 'uid');
    }
}
